package com.agriculture.monitoring.service;

import com.agriculture.monitoring.dto.PathPointDTO;
import com.agriculture.monitoring.entity.GeoDevice;
import com.agriculture.monitoring.entity.GeoPathPoint;
import com.agriculture.monitoring.repository.DeviceRepository;
import com.agriculture.monitoring.repository.PathPointRepository;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.GeometryFactory;
import org.locationtech.jts.geom.Point;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
public class PathPointService {
    
    @Autowired
    private PathPointRepository pathPointRepository;
    
    @Autowired
    private DeviceRepository deviceRepository;
    
    private GeometryFactory geometryFactory = new GeometryFactory();
    
    public List<PathPointDTO> getPointsByDeviceId(Long deviceId) {
        return pathPointRepository.findByDeviceIdOrderByRecordTime(deviceId).stream()
                .map(this::convertToDTO)
                .collect(Collectors.toList());
    }
    
    public List<PathPointDTO> getPointsByPathId(Long pathId) {
        return pathPointRepository.findByPathIdOrderByRecordTime(pathId).stream()
                .map(this::convertToDTO)
                .collect(Collectors.toList());
    }
    
    public List<PathPointDTO> getPointsByDeviceAndTimeRange(Long deviceId, 
                                                           LocalDateTime startTime, 
                                                           LocalDateTime endTime) {
        return pathPointRepository.findByDeviceIdAndTimeRange(deviceId, startTime, endTime).stream()
                .map(this::convertToDTO)
                .collect(Collectors.toList());
    }
    
    public PathPointDTO createPathPoint(PathPointDTO pointDTO) {
        Optional<GeoDevice> device = deviceRepository.findById(pointDTO.getDeviceId());
        if (!device.isPresent()) {
            throw new IllegalArgumentException("设备不存在");
        }
        
        GeoPathPoint point = new GeoPathPoint();
        point.setDevice(device.get());
        point.setLongitude(pointDTO.getLongitude());
        point.setLatitude(pointDTO.getLatitude());
        
        // 创建空间点
        Point geometryPoint = geometryFactory.createPoint(
            new Coordinate(pointDTO.getLongitude(), pointDTO.getLatitude())
        );
        point.setPointGeometry(geometryPoint);
        
        point.setRecordTime(pointDTO.getRecordTime() != null ? 
            pointDTO.getRecordTime() : LocalDateTime.now());
        point.setSpeed(pointDTO.getSpeed());
        point.setDirection(pointDTO.getDirection());
        
        GeoPathPoint saved = pathPointRepository.save(point);
        return convertToDTO(saved);
    }
    
    public void batchCreatePathPoints(List<PathPointDTO> pointDTOs) {
        List<GeoPathPoint> points = pointDTOs.stream()
                .map(dto -> {
                    GeoPathPoint point = new GeoPathPoint();
                    // 设置属性...
                    return point;
                })
                .collect(Collectors.toList());
        
        pathPointRepository.saveAll(points);
    }
    
    private PathPointDTO convertToDTO(GeoPathPoint point) {
        PathPointDTO dto = new PathPointDTO();
        dto.setId(point.getId());
        dto.setDeviceId(point.getDevice().getId());
        dto.setLongitude(point.getLongitude());
        dto.setLatitude(point.getLatitude());
        dto.setRecordTime(point.getRecordTime());
        dto.setSpeed(point.getSpeed());
        dto.setDirection(point.getDirection());
        return dto;
    }
}